package com.zheng.study.base.utils;


import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 发送http请求的工具类
 *
 * @author yangmin
 */
public class HttpRequestUtil {
	/**
	 * 发送get请求
	 *
	 * @param url
	 * @return String
	 */
	public String sendHttpRequest(String url) {
		StringBuilder stringBuilder = null;
		CloseableHttpClient httpclient = HttpClients.createDefault();
		RequestConfig requestConfig = RequestConfig.custom()//设置请求超时等
				.setSocketTimeout(20000)
				.setConnectTimeout(20000)
				.build();
		HttpGet httpGet = new HttpGet(url);//创建一个get请求
		httpGet.setConfig(requestConfig);//应用设置
		try {
			CloseableHttpResponse response = httpclient.execute(httpGet);//提交请求
			HttpEntity entity = response.getEntity();//获得响应实体
			BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
			stringBuilder = new StringBuilder();
			String line = null;
			/*
			 * 拼接响应内容
			 */
			while ((line = reader.readLine()) != null) {
				stringBuilder.append(line + "\n");
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
		return stringBuilder.toString();
	}

	/**
	 * 发送post请求
	 *
	 * @param url
	 * @param params
	 * @return String
	 */
	public String sendHttpRequest(String url, String params) {
		StringBuilder stringBuilder = null;
		CloseableHttpClient httpclient = HttpClients.createDefault();
		RequestConfig requestConfig = RequestConfig.custom()
				.setSocketTimeout(20000)
				.setConnectTimeout(20000)
				.build();
		HttpPost httpPost = new HttpPost(url);//创建一个post请求
		httpPost.setConfig(requestConfig);//应用设置
		try {
			/*
			 * 设置请求参数,编码等
			 */
			StringEntity stringEntity = new StringEntity(params, "utf-8");
			stringEntity.setContentType("application/json");
			stringEntity.setContentEncoding("utf-8");
			httpPost.setEntity(stringEntity);

			CloseableHttpResponse response = httpclient.execute(httpPost);//提交请求
			HttpEntity entity = response.getEntity();
			BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
			stringBuilder = new StringBuilder();
			String line = null;
			/*
			 * 拼接响应内容
			 */
			while ((line = reader.readLine()) != null) {
				stringBuilder.append(line + "\n");
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
		return stringBuilder.toString();
	}


	/**
	 * 发送post请求
	 *
	 * @param url
	 * @param params
	 * @return String
	 */
	public String sendHttpRequest(String url, String params, String contentType) {
		StringBuilder stringBuilder = null;
		CloseableHttpClient httpclient = HttpClients.createDefault();
		RequestConfig requestConfig = RequestConfig.custom()
				.setSocketTimeout(20000)
				.setConnectTimeout(20000)
				.build();
		HttpPost httpPost = new HttpPost(url);//创建一个post请求
		httpPost.setConfig(requestConfig);//应用设置
		try {
			/*
			 * 设置请求参数,编码等
			 */
			StringEntity stringEntity = new StringEntity(params, "utf-8");
			stringEntity.setContentType(contentType);
			stringEntity.setContentEncoding("utf-8");
			httpPost.setEntity(stringEntity);

			CloseableHttpResponse response = httpclient.execute(httpPost);//提交请求
			HttpEntity entity = response.getEntity();
			BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
			stringBuilder = new StringBuilder();
			String line = null;
			/*
			 * 拼接响应内容
			 */
			while ((line = reader.readLine()) != null) {
				stringBuilder.append(line + "\n");
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
		return stringBuilder.toString();
	}

	/**
	 * 获取客户端帧时ip
	 */
	public static String getIpAddress(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_CLIENT_IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_X_FORWARDED_FOR");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

}
